package com.uniview.controller;

import com.uniview.dto.RespDTO;
import com.uniview.service.oauth.OauthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author lux
 * @date: 2019/11/13
 * @desc: 客户端认证逻辑
 */
@RestController
@RequestMapping("/oauth2.0")
public class OauthController {
    @Autowired
    OauthService oauthService;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * @desc 获取authorizationCode
     * @param clientId 客户端id
     * @param scope 授权范围
     * @param status 状态
     * @param redirectUri 重定向地址
     */
    @RequestMapping("/authorize")
    public RespDTO<String> authorize(@RequestParam("clientId") String clientId,
                          @RequestParam("scope") String scope,
                          @RequestParam("status") String status,
                          @RequestParam("redirectUri") String redirectUri){
        String uri = oauthService.getAuthorizationCode(clientId,scope,status,redirectUri);
        return RespDTO.onSuc(uri);
    }

    /**
     * @desc 获取token
     * @param clientId
     * @param code
     * @param grantType
     * @param clientSecret
     * @param redirectUri
     */
    @RequestMapping("/token")
    public RespDTO<String> getToken(@RequestParam("clientId") String clientId,
                                    @RequestParam("code") String code,
                                    @RequestParam("grantType") String grantType,
                                    @RequestParam("clientSecret") String clientSecret,
                                    @RequestParam("redirectUri") String redirectUri){
        Map<String,Object> resMap = oauthService.getAccessToken(clientId,code,grantType,clientSecret,redirectUri);
        return RespDTO.onSuc(resMap);
    }

    /**
     * @desc 刷新token
     * @param refreshToken
     */
    @RequestMapping("/refreshToken")
    public RespDTO<String> refreshToken(@RequestParam("refreshToken") String refreshToken){
        String accessToken = oauthService.refreshToken(refreshToken);
        return RespDTO.onSuc(accessToken);
    }

}
